package com.hmkj.quote.queue;

import com.hmkj.boot.utils.SpringContextUtils;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.po.order.OrderSimulate;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.order.OrderSimulateService;
import lombok.extern.slf4j.Slf4j;

/**
 * 流单操作队列线程
 *
 * @author eproo
 * @date 2017年03月16日 21:38
 */
@Slf4j
public class FlowOrderSimulateQueueThread implements Runnable {


    private RedisService redisService = SpringContextUtils.getBean(RedisService.class);
    private OrderSimulateService orderSimulateService = SpringContextUtils.getBean(OrderSimulateService.class);

    @Override
    public void run() {
        while (true) {
            try {
                if (startQueue()) {
                    Thread.sleep(1000);
                }
            } catch (Exception e) {
                log.error(e.getMessage());
                e.printStackTrace();
            }
        }
    }

    private boolean startQueue() throws Exception {
        boolean isFinish = false;
        try {
            OrderSimulate orderSimulate = redisService.pop(CacheID.QUEUE_FLOW_ORDER_SL,OrderSimulate.class);
            if (null == orderSimulate) {
                return true;
            }
            log.info("处理模拟订单流单，模拟订单号："+orderSimulate.getOrderNo());
            orderSimulateService.flowOrder(orderSimulate);
        } catch (Exception e) {
            log.error("执行模拟订单流单队列失败：" + e.getMessage());
            e.printStackTrace();
        }
        return isFinish;
    }
}
